package com.huitone.smspfm.interfacz.model;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.bin.common.utils.StringUtils;
import com.huitone.smspfm.interfacz.logger.ExtLogger;

/**
 * @Title SmsSpeed.java
 * @Package com.huitone.smspfm.interfacz.model
 * @Description 短信接入流速统计
 * @author ozh
 * @date
 * @version V1.0
 */
public class SmsSpeed1 {
	private static final ExtLogger logger = ExtLogger.create(SmsSpeed1.class);
	private static DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
	private long monthCnt = 0;
	private long dayCnt = 0;
	private long hourCnt = 0;
	private long minuteCnt = 0;
	private long secondCnt = 0;

	// date
//	private int year = 0;
//	private int month = 0;
//	private int day = 0;
//	private int hour = 0;
//	private int minute = 0;
//	private int second = 0;
	
	
	
	private String resptime = "";
	
	public String getResptime() {
		return resptime;
	}
	public void setResptime(String resptime) {
		this.resptime = resptime;
	}
	private int getYear(){
		if(StringUtils.isEmpty(this.resptime) && this.resptime.length() < 14){
			return 0;
		}
		return StringUtils.toInteger(this.resptime.subSequence(0, 4));	
	}
	private int getMonth(){
		if(StringUtils.isEmpty(this.resptime) && this.resptime.length() < 14){
			return 0;
		}
		return StringUtils.toInteger(this.resptime.subSequence(4, 6));	
	}
	private int getDay(){
		if(StringUtils.isEmpty(this.resptime) && this.resptime.length() < 14){
			return 0;
		}
		return StringUtils.toInteger(this.resptime.subSequence(6, 8));	
	}
	private int getHour(){
		if(StringUtils.isEmpty(this.resptime) && this.resptime.length() < 14){
			return 0;
		}
		return StringUtils.toInteger(this.resptime.subSequence(8, 10));	
	}
	private int getMinute(){
		if(StringUtils.isEmpty(this.resptime) && this.resptime.length() < 14){
			return 0;
		}
		return StringUtils.toInteger(this.resptime.subSequence(10, 12));	
	}
	private int getSecond(){
		if(StringUtils.isEmpty(this.resptime) && this.resptime.length() < 14){
			return 0;
		}
		return StringUtils.toInteger(this.resptime.subSequence(12, 14));	
	}
	
	private Object mLock = new Object();

	public SmsSpeed1() {
		Calendar cal = Calendar.getInstance();
//		this.year = cal.get(Calendar.YEAR);//年份
//		this.month = cal.get(Calendar.MONTH);// 获取月份
//		this.day = cal.get(Calendar.DATE);// 获取日
//		this.hour = cal.get(Calendar.HOUR);// 小时
//		this.minute = cal.get(Calendar.MINUTE);// 分
//		this.second = cal.get(Calendar.SECOND);// 秒
		this.resptime = dateFormat.format(cal.getTime());
	}
	
	public static SmsSpeed1 parse(String msg){
		SmsSpeed1 smsSpeed = new SmsSpeed1();
		String[] msgs =  msg.split(":");
		try {
			if(msgs.length >= 6){
				smsSpeed.setMonthCnt(StringUtils.tolong(msgs[0]));
				smsSpeed.setDayCnt(StringUtils.tolong(msgs[1]));
				smsSpeed.setHourCnt(StringUtils.tolong(msgs[2]));
				smsSpeed.setMinuteCnt(StringUtils.tolong(msgs[3]));
				smsSpeed.setSecondCnt(StringUtils.tolong(msgs[4]));				
			} else {
				smsSpeed = null;
			}
		} catch (Exception e) {
			smsSpeed = null;
		}
		
		return smsSpeed;
	}

	public long getMonthCnt() {
		return monthCnt;
	}

	public void setMonthCnt(long monthCnt) {
		this.monthCnt = monthCnt;
	}

	public long getDayCnt() {
		return dayCnt;
	}

	public void setDayCnt(long dayCnt) {
		this.dayCnt = dayCnt;
	}

	public long getHourCnt() {
		return hourCnt;
	}

	public void setHourCnt(long hourCnt) {
		this.hourCnt = hourCnt;
	}

	public long getMinuteCnt() {
		return minuteCnt;
	}

	public void setMinuteCnt(long minuteCnt) {
		this.minuteCnt = minuteCnt;
	}

	public long getSecondCnt() {
		return secondCnt;
	}

	public void setSecondCnt(long secondCnt) {
		this.secondCnt = secondCnt;
	}

	public Map<String, String> getMap() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("monthCnt", String.valueOf(this.monthCnt));
		map.put("dayCnt", String.valueOf(this.dayCnt));
		map.put("hourCnt", String.valueOf(this.hourCnt));
		map.put("minuteCnt", String.valueOf(this.minuteCnt));
		map.put("secondCnt", String.valueOf(this.secondCnt));
		map.put("respTime", this.resptime);
		return map;
	}

	public String toRedisString() {
		StringBuffer sb = new StringBuffer();
		sb.append(String.valueOf(this.monthCnt)).append(":")
				.append(String.valueOf(this.dayCnt)).append(":")
				.append(String.valueOf(this.hourCnt)).append(":")
				.append(String.valueOf(this.minuteCnt)).append(":")
				.append(String.valueOf(this.secondCnt)).append(":")
				.append(this.resptime);
		return sb.toString();
	}

	public SmsSpeed1 add() {
		synchronized (mLock) {
			Calendar cal = Calendar.getInstance();
			int year = cal.get(Calendar.YEAR);// 年份
			int month = cal.get(Calendar.MONTH)+1;// 获取月份
			int day = cal.get(Calendar.DATE);// 获取日
			int hour = cal.get(Calendar.HOUR);// 小时
			int minute = cal.get(Calendar.MINUTE);// 分
			int second = cal.get(Calendar.SECOND);// 秒

			if (second == this.getSecond()) {
				secondCnt++;
			} else { secondCnt = 0; }
			if (minute == this.getMinute()) {
				minuteCnt++;
			} else { minuteCnt = 0; }
			if (hour == this.getHour()) {
				hourCnt++;
			} else { hourCnt = 0; }
			if (day == this.getDay()) {
				dayCnt++;
			} else { dayCnt = 0; }
			if (month == this.getMonth()) {
				monthCnt++;
			} else { monthCnt = 0; }

			setResptime(dateFormat.format(cal.getTime()));
			return this;
		}
	}

	public boolean isTimeOut(long time, int smsSpeedTimeOut) {
		if (time == 0 || smsSpeedTimeOut <= 0) {
			return true;
		}

		boolean result = false;
//		StringBuffer sb = new StringBuffer();
//		sb.append(StringUtils.int2String(4,this.year)).append("-")
//			.append(StringUtils.int2String(2,this.month)).append("-")
//			.append(StringUtils.int2String(2,this.day)).append(" ")
//			.append(StringUtils.int2String(2,this.hour)).append(":")
//			.append(StringUtils.int2String(2,this.minute)).append(":")
//			.append(StringUtils.int2String(2,this.second));
		
		try {
//			Date date = dateFormat.parse(sb.toString());
			Date date = dateFormat.parse(resptime);
			result = (date.getTime() + smsSpeedTimeOut) > time;
		} catch (ParseException e) {
			logger.error("Date conversion error");
		}

		return result;
	}
}
